#include "momotamusic.h"
#include "ui_momotamusic.h"
#include <QMouseEvent>
#include <QGraphicsDropShadowEffect>
#include <QDebug>
#include <QJsonObject>
#include <QJsonArray>
MomotaMusic::MomotaMusic(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MomotaMusic)
{
    ui->setupUi(this);
    initUi();
    connectSignalAndSlots();
}



MomotaMusic::~MomotaMusic()
{
    delete ui;
}

void MomotaMusic::initUi()
{
    this->setWindowFlag(Qt::FramelessWindowHint);   // 设置为无边框
    setAttribute(Qt::WA_TranslucentBackground);// 设置背景透明

    // 给窗口设置阴影效果
    QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(0,0);
    shadowEffect->setColor(QColor(0,0,0));      //黑色
    shadowEffect->setBlurRadius(10);
    this->setGraphicsEffect(shadowEffect);  //显示阴影

    // 设置BtForm图标及文本信息并发送按钮对应的page页面索引
    ui->rec->setIconAndText(":/image/rec.png","推荐",0);
    ui->radio->setIconAndText(":/image/radio.png","电台",1);
    ui->music->setIconAndText(":/image/music.png","音乐馆",2);
    ui->like->setIconAndText(":/image/like.png","我的喜欢",3);
    ui->local->setIconAndText(":/image/local.png","本地下载",4);
    ui->recent->setIconAndText(":/image/recent.png","最近播放",5);
    // 初始化显示我的喜欢页面
    ui->stackedWidget->setCurrentIndex(3);

    // 初始化推荐页面
    srand(time(NULL));  //添加随机数种子
    // 将图片路径和文本和页面需要多少行发送给recbox的initRecBoxUi函数进行处理。
    ui->recMusicBox->initRecBoxUi(randomPiction(),1);
    ui->supplyMusicBox->initRecBoxUi(randomPiction(),2);

    // 设置CommonPage的信息
    ui->LikePage->setCommonPageUI("我的喜欢",":/image/mylikebg.png");
    ui->LocalPage->setCommonPageUI("本地音乐",":/image/localbg.png");
    ui->RecentPage->setCommonPageUI("最近播放",":/image/recentbg.png");

}

QJsonArray MomotaMusic::randomPiction()
{   // 加载图片资源到vector中，方便后续调用
    QVector<QString> vecImageName;
    for (int i = 1; i<40;i++ ) {
        QString str;
        // arg参数讲解：1、填充内容； 2、i占3位； 3、10进制； i不够3位用0填充；
        str = QString("%1.png").arg(i, 3, 10, QChar('0'));
        vecImageName.append(str);
    }
    // 打乱顺序
    std::random_shuffle(vecImageName.begin(),vecImageName.end());


    QJsonArray objArray;

    for(int i = 1;i<vecImageName.size();i++)
    {
        QJsonObject obj;
        obj.insert("path",":/image/rec/"+vecImageName[i]);
        QString strText;
        // arg参数讲解：1、填充内容； 2、i占3位； 3、10进制； i不够3位用0填充；
        strText = QString("推荐—%1").arg(i, 3, 10, QLatin1Char('0'));
        obj.insert("text",strText);
        objArray.append(obj);
    }
    return objArray;
}

void MomotaMusic::connectSignalAndSlots()
{   // 关联BtForm的信号和处理信号的槽函数
    connect(ui->rec,&BtForm::btClick,this,&MomotaMusic::onBtFormClick);
    connect(ui->radio,&BtForm::btClick,this,&MomotaMusic::onBtFormClick);
    connect(ui->music,&BtForm::btClick,this,&MomotaMusic::onBtFormClick);
    connect(ui->like,&BtForm::btClick,this,&MomotaMusic::onBtFormClick);
    connect(ui->local,&BtForm::btClick,this,&MomotaMusic::onBtFormClick);
    connect(ui->recent,&BtForm::btClick,this,&MomotaMusic::onBtFormClick);

}


void MomotaMusic::on_quit_clicked()
{
    close();
}

void MomotaMusic::onBtFormClick(int pageId)
{
    // 清除之前的BtForm的背景色
    // 1.获取所有的BtForm
    QList<BtForm*> BtFormList = this->findChildren<BtForm*>();
    for(auto btForm : BtFormList)
    {
        if(btForm->getPageId() != pageId){  // 若不是当前BtForm的pageId
            btForm->clearBackground();      // 则清除背景色 只保留当前点击的BtForm变色
            btForm->hideAnimal();           // 隐藏动画效果
        }
        else
            btForm->showAnimal();           // 点击的BtForm显示动画效果
    }
    qDebug() << "切换页面" << pageId;
    ui->stackedWidget->setCurrentIndex(pageId);
}

void MomotaMusic::mouseMoveEvent(QMouseEvent *event)
{// 鼠标移动 move窗口
    if(Qt::LeftButton == event->buttons())
    {
        move(event->globalPos() - dragPosition);
        return;
    }
    QWidget::mouseMoveEvent(event);
}

void MomotaMusic::mousePressEvent(QMouseEvent *event)
{// 鼠标点击记录坐标
    if(Qt::LeftButton == event->button())
    {
        // 获取鼠标相对于屏幕的全局坐标
        dragPosition = event->globalPos() - geometry().topLeft();
        return;
    }
    QWidget::mousePressEvent(event);

}

